home *** CD-ROM | disk | FTP | other *** search
- Path: antares.dfma.com!news
- From: johnb@dfma.com (John Breckenridge)
- Newsgroups: comp.lang.c
- Subject: Re: What's better & why
- Date: Fri, 23 Feb 1996 19:52:03 GMT
- Organization: Boothroyd Dewhurst, Inc.
- Message-ID: <4gkrhj$4dq@antares.dfma.com>
- References: <31297C5A.E6C@connix.com>
- NNTP-Posting-Host: johnb.dfma.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Scott Hawley <shawley@connix.com> wrote:
- >I was just curious what you all though about the following code.
- >Please tell me what is better (faster/Smaller) and why. Or does it make
- >any difference at all. I just though about this while I was programming
- >today?
-
- >example 1:
- > val = 1;
- > if( what ever...)val = 0;
-
- >or
- >example 2:
- > if( what ever... )val = 0;
- > else val = 1;
-
- >amy remarks?
- I'm not Amy, but I'll remark anyway. (-:
-
- When whatever is True, example 1 performs 2 assignments on val.
- Example 2 will always perform only 1 assignment on val.
-
- So the question boils down to whether the extra assignment is
- faster than the branch for the else.
-
- Without looking at the generated assembly instructions and
- running with a stopwatch, I can't say which statement is
- better (faster/smaller) with your compiler. It is possible
- that a good optimizer would generate the same instructions for
- both. The only way to find out is to try it and see.
-
- From a style point of view, I find example 2 slightly more
- readable.
-
- At any rate, I don't think you'd save anything by using:
- val = (what ever...) ? 0 : 1;
-
-
-
-
- John Breckenridge Boothroyd Dewhurst, Inc.
- johnb@dfma.com all opinions are my own
-
-